package com.example.administrator.baikangxing.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.adapter.HeartAdapter;
import com.example.administrator.baikangxing.bean.HeartBean;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.CommomUtil;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.LogUtil;
import com.example.administrator.baikangxing.utils.ToastUtil;
import com.example.administrator.baikangxing.utils.Utils;
import com.example.administrator.baikangxing.view.dashedprogress.DashedCircularProgress;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class HeartActivity extends BaseActivity implements View.OnClickListener {


    private DashedCircularProgress heart_dashed_pro;
    private TextView heart_tv_number;
    private ListView heart_lv_history;
    private TextView heart_tv_renovate;
    private ArrayList<HeartBean> list;
    private String select_num;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_heart;
    }

    @Override
    public void initView() {
        base_ll_title.setBackgroundResource(R.color.heart_bg);
        setTitles("心率");
        base_ib_menu.setVisibility(View.INVISIBLE);
        heart_dashed_pro = findView(R.id.heart_dashed_pro);
        heart_tv_number = findView(R.id.heart_tv_number);
        heart_lv_history = findView(R.id.heart_lv_history);
        heart_tv_renovate = findView(R.id.heart_tv_renovate);
        heart_tv_renovate.setOnClickListener(this);
    }

    @Override
    public void initListener() {
        heart_lv_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HeartActivity.this, DeclareActivity.class);
                intent.putExtra(Constants.HEART_NUM, Utils.stringToInt(list.get(i).getAvgheartrate())+"");
                intent.putExtra(Constants.HEART_TIME,list.get(i).getTime()+":00");
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String HeartRate = intent.getStringExtra(Constants.HEALTH_TARGET[0]);
        heart_dashed_pro.setValue(Float.parseFloat(HeartRate));
        heart_tv_number.setText(HeartRate);
        getDataFromNet();
    }

    private void getDataFromNet() {//获取历史数据
        String url = Url.base_url+Constants.HEALTH_TARGET[0];
        select_num = OperateData.getStringData(this, Constants.SELECT_DEVICE_NUM);
        String[] values = new String[]{select_num,"hour","0","23"};
        HttpUtil.getInstance().postString(url, new String[]{"devid", "ext", "begin", "end"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        JSONArray message = successString.getJSONArray("message");
                        list = CommomUtil.parserJsonArrayToList(message, HeartBean.class);
                        HeartAdapter adapter = new HeartAdapter(list);
                        heart_lv_history.setAdapter(adapter);
                    }else if(successString.getString("errorCode").equals("105")){
                        ToastUtil.showToast("暂无数据");
                    }else {
                        ToastUtil.showToast(successString.getString("errorinfo"));
                        LogUtil.e("请求失败:"+successString.getString("errorinfo"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.heart_tv_renovate:
                getHeart();
                break;
            default:
                break;
        }
    }

    private void getHeart() {
        String[] values = new String[]{select_num,"ext"};
        HttpUtil.getInstance().postString(Url.get_heart, new String[]{"devid", "ext"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        ToastUtil.showToast("已开始获取心率，请稍候");
                    }else {
                        ToastUtil.showToast("获取血压失败，请稍后重试");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }
}
